import ObjBase = require('./../obj/ObjBase');
import Serializer = require('./../base/Serializer');
import GlobalData = require('./../../z/GlobalData');
import DEFINE = require('./../../common/DEFINE');
import SJGReplaceGroup=require('./../replace/SJGReplaceGroup');
class UIObjBase{public kernelObjId=0;};
export  = ObjManager;

/**
 *
 * @author 
 *
 */
class ObjManager extends Serializer{
    private map: Object = {}; // 存储内核对象
    private map2: Object = {}; // 存储UI对象
    private map3: Object={}; // 存储可替换数组
	public constructor(g?) {
        super(g);
	}
	// GroupObj、ImageObj、TextObj等对象都需要加入对象管理器
	// 如果对象含有id切forceNew为false，则不会重新分配id
	public saveObject(obj:ObjBase, forceNew:boolean=false):number{
        var id: number = obj.id;
        if(forceNew){
            // 获取新id
            id=this.getGlobalData().idGenerator().newId();
        }else{
            if(id==DEFINE.ID_NONE) {
                // 获取新id
                id=this.getGlobalData().idGenerator().newId();
            } else {
                if(id>this.getGlobalData().idGenerator().curId()) {
                    this.getGlobalData().idGenerator().initId(id);
                }
            }
        }

        obj.id = id;
        this.map[id] = obj;
        return id;
	}
	public getObject(id:number):ObjBase{
        return this.map[id];
	}
	public releaseObject(obj:ObjBase){
        obj.id = DEFINE.ID_NONE;
        this.map[obj.id] = null;
	}

	
    // UIGroupObj、UIImageObj、UITextObj等对象都需要加入对象管理器
    public saveUIObject(obj:UIObjBase):number{
        this.map2[obj.kernelObjId] = obj;
        return obj.kernelObjId;
    }
    public getUIObject(id:number):UIObjBase{
        return this.map2[id];
    }
    public releaseUIObject(obj:UIObjBase){
        obj.kernelObjId = DEFINE.ID_NONE;
        this.map2[obj.kernelObjId] = null;
    }
    
    // UIGroupObj、UIImageObj、UITextObj等对象都需要加入对象管理器
    public saveReplaceGroupObject(obj:SJGReplaceGroup):number{
        this.map3[obj.id] = obj;
        return obj.id;
    }
    public getReplaceGroupObject(id: number): SJGReplaceGroup{
        return this.map3[id];
    }
    public getAllIdsOfReplaceGroupObj():Array<number>{
        var ret: Array<number>=new Array<number>();
        for(var key in this.map3){ 
            if(/^[\d.]+$/.test(key)){
                ret.push(key);
            }
        }
        return ret;
    }
    public releaseReplaceGroupObject(obj:SJGReplaceGroup){
        obj.id = DEFINE.ID_NONE;
        this.map3[obj.id] = null;
    }
//SJG_MARK_APPEND--------------------


}//SJG_MARK_END-------------------------